package com.example.rbacmanage.util;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;

public class ExcelUtil {

    public static void readExcel(String filePath){
        try(FileInputStream fileInputStream = new FileInputStream(filePath)){
            Workbook workbook = new XSSFWorkbook(fileInputStream);
            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                for (Cell cell : row) {
                    switch (cell.getCellType()) {
                        case STRING:
                            System.out.println(cell.getStringCellValue());
                            break;
                        case NUMERIC:
                            System.out.println(cell.getNumericCellValue());
                            break;
                        case BOOLEAN:
                            System.out.println(cell.getBooleanCellValue());
                            break;
                        default:
                            break;
                    }
                }
                System.out.println();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void writeExcel(String filePath){
        try(Workbook workbook = new XSSFWorkbook();
                FileOutputStream fileOutputStream = new FileOutputStream(filePath)){
            Sheet sheet = workbook.createSheet("sheet1");
            Object[][] data={
                    {"ID","Name","Value"},
                    {1,"ss",23},
                    {2,"ssf",24},
            };
            int rowCount = 0;
            for (Object[] objects : data) {
                Row row = sheet.createRow(rowCount++);
                int columnCount = 0;
                for (Object object : objects) {
                    Cell cell = row.createCell(columnCount++);
                    if (object instanceof Integer) {
                        cell.setCellValue((Integer) object);
                    }else if (object instanceof String) {
                        cell.setCellValue((String) object);
                    }else if (object instanceof Double) {
                        cell.setCellValue((Double) object);
                    }else if (object instanceof Boolean) {
                        cell.setCellValue((Boolean) object);
                    }else if (object instanceof Date) {
                        cell.setCellValue((Date) object);
                    }else{
                        cell.setCellValue(object.toString());
                    }
                }
            }
            workbook.write(fileOutputStream);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
//        writeExcel("C:\\Users\\xiezh\\Documents\\test.xlsx");
        readExcel("C:\\Users\\xiezh\\Documents\\test.xlsx");
    }
}
